﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Election.ExpelClanFromKingdomDecision
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.Party.PartyComponents;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Election
{
  public class ExpelClanFromKingdomDecision : KingdomDecision
  {
    private const float ClanFiefModifier = 0.005f;
    [SaveableField(100)]
    public readonly Clan ClanToExpel;
    [SaveableField(102)]
    public readonly Kingdom OldKingdom;

    internal static void AutoGeneratedStaticCollectObjectsExpelClanFromKingdomDecision(
      object o,
      List<object> collectedObjects)
    {
      ((KingdomDecision) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.ClanToExpel);
      collectedObjects.Add((object) this.OldKingdom);
    }

    internal static object AutoGeneratedGetMemberValueClanToExpel(object o)
    {
      return (object) ((ExpelClanFromKingdomDecision) o).ClanToExpel;
    }

    internal static object AutoGeneratedGetMemberValueOldKingdom(object o)
    {
      return (object) ((ExpelClanFromKingdomDecision) o).OldKingdom;
    }

    public ExpelClanFromKingdomDecision(Clan proposerClan, Clan clan)
      : base(proposerClan)
    {
      this.ClanToExpel = clan;
      this.OldKingdom = clan.Kingdom;
    }

    public override bool IsAllowed()
    {
      return Campaign.Current.Models.KingdomDecisionPermissionModel.IsExpulsionDecisionAllowed(this.ClanToExpel);
    }

    public override int GetProposalInfluenceCost()
    {
      return Campaign.Current.Models.DiplomacyModel.GetInfluenceCostOfExpellingClan(this.ProposerClan);
    }

    public override TextObject GetGeneralTitle()
    {
      TextObject generalTitle = new TextObject("{=pF92DagG}Expel {CLAN_NAME} from {KINGDOM_NAME}");
      generalTitle.SetTextVariable("CLAN_NAME", this.ClanToExpel.Name);
      generalTitle.SetTextVariable("KINGDOM_NAME", this.OldKingdom.Name);
      return generalTitle;
    }

    public override TextObject GetSupportTitle()
    {
      TextObject supportTitle = new TextObject("{=ZwpWX8Zx}Vote for expelling {CLAN_NAME} from the kingdom");
      supportTitle.SetTextVariable("CLAN_NAME", this.ClanToExpel.Name);
      return supportTitle;
    }

    public override TextObject GetChooseTitle()
    {
      TextObject chooseTitle = new TextObject("{=pF92DagG}Expel {CLAN_NAME} from {KINGDOM_NAME}");
      chooseTitle.SetTextVariable("CLAN_NAME", this.ClanToExpel.Name);
      chooseTitle.SetTextVariable("KINGDOM_NAME", this.OldKingdom.Name);
      return chooseTitle;
    }

    public override TextObject GetSupportDescription()
    {
      TextObject supportDescription = new TextObject("{=eTr0XHas}{FACTION_LEADER} will decide if {CLAN_NAME} will be expelled from {KINGDOM_NAME}. You can pick your stance regarding this decision.");
      supportDescription.SetTextVariable("FACTION_LEADER", this.DetermineChooser().Leader.Name);
      supportDescription.SetTextVariable("CLAN_NAME", this.ClanToExpel.Name);
      supportDescription.SetTextVariable("KINGDOM_NAME", this.OldKingdom.Name);
      return supportDescription;
    }

    public override TextObject GetChooseDescription()
    {
      TextObject chooseDescription = new TextObject("{=J8brFxIW}As {?IS_FEMALE}queen{?}king{\\?} you must decide if {CLAN_NAME} will be expelled from kingdom.");
      chooseDescription.SetTextVariable("IS_FEMALE", this.DetermineChooser().Leader.IsFemale ? 1 : 0);
      chooseDescription.SetTextVariable("CLAN_NAME", this.ClanToExpel.Name);
      return chooseDescription;
    }

    public override IEnumerable<DecisionOutcome> DetermineInitialCandidates()
    {
      yield return (DecisionOutcome) new ExpelClanFromKingdomDecision.ExpelClanDecisionOutcome(true);
      yield return (DecisionOutcome) new ExpelClanFromKingdomDecision.ExpelClanDecisionOutcome(false);
    }

    public override Clan DetermineChooser() => this.OldKingdom.RulingClan;

    protected override bool ShouldBeCancelledInternal()
    {
      return !this.Kingdom.Clans.Contains(this.ClanToExpel);
    }

    public override float DetermineSupport(Clan clan, DecisionOutcome possibleOutcome)
    {
      bool shouldBeExpelled = ((ExpelClanFromKingdomDecision.ExpelClanDecisionOutcome) possibleOutcome).ShouldBeExpelled;
      float num1 = 3.5f;
      float num2 = (float) FactionManager.GetRelationBetweenClans(this.ClanToExpel, clan) * num1;
      float num3 = 0.0f;
      float num4 = 0.0f;
      float num5 = 0.0f;
      float num6 = 10000f;
      foreach (Settlement settlement in (List<Settlement>) this.ClanToExpel.Settlements)
        num3 += settlement.GetSettlementValueForFaction((IFaction) this.OldKingdom) * 0.005f;
      if (clan.Leader.GetTraitLevel(DefaultTraits.Calculating) > 0)
        num5 = (float) ((double) this.ClanToExpel.Influence * 0.05000000074505806 + (double) this.ClanToExpel.Renown * 0.019999999552965164);
      if (clan.Leader.GetTraitLevel(DefaultTraits.Commander) > 0)
      {
        foreach (WarPartyComponent warPartyComponent in (List<WarPartyComponent>) this.ClanToExpel.WarPartyComponents)
          num4 += (float) warPartyComponent.MobileParty.MemberRoster.TotalManCount * 0.01f;
      }
      float num7 = num6 + num2 + num3 + num4 + num5;
      return !shouldBeExpelled ? num7 : -num7;
    }

    public override void DetermineSponsors(MBReadOnlyList<DecisionOutcome> possibleOutcomes)
    {
      foreach (DecisionOutcome possibleOutcome in (List<DecisionOutcome>) possibleOutcomes)
      {
        if (((ExpelClanFromKingdomDecision.ExpelClanDecisionOutcome) possibleOutcome).ShouldBeExpelled)
          possibleOutcome.SetSponsor(this.ProposerClan);
        else
          this.AssignDefaultSponsor(possibleOutcome);
      }
    }

    public override void ApplyChosenOutcome(DecisionOutcome chosenOutcome)
    {
      if (!((ExpelClanFromKingdomDecision.ExpelClanDecisionOutcome) chosenOutcome).ShouldBeExpelled)
        return;
      int expellingClanFromKingdom = Campaign.Current.Models.DiplomacyModel.GetRelationCostOfExpellingClanFromKingdom();
      foreach (Supporter supporter in chosenOutcome.SupporterList)
      {
        if (((ExpelClanFromKingdomDecision.ExpelClanDecisionOutcome) chosenOutcome).ShouldBeExpelled && this.ClanToExpel.Leader != supporter.Clan.Leader)
          ChangeRelationAction.ApplyRelationChangeBetweenHeroes(this.ClanToExpel.Leader, supporter.Clan.Leader, expellingClanFromKingdom);
      }
      ChangeKingdomAction.ApplyByLeaveKingdom(this.ClanToExpel);
    }

    public override TextObject GetSecondaryEffects()
    {
      return new TextObject("{=fJY9uosa}All supporters gain some relations with each other and lose a large amount of relations with the expelled clan.");
    }

    public override void ApplySecondaryEffects(
      MBReadOnlyList<DecisionOutcome> possibleOutcomes,
      DecisionOutcome chosenOutcome)
    {
    }

    public override TextObject GetChosenOutcomeText(
      DecisionOutcome chosenOutcome,
      KingdomDecision.SupportStatus supportStatus,
      bool isShortVersion = false)
    {
      TextObject parent;
      if (((ExpelClanFromKingdomDecision.ExpelClanDecisionOutcome) chosenOutcome).ShouldBeExpelled)
      {
        if (this.IsSingleClanDecision())
        {
          parent = new TextObject("{=h5eTEYON}{RULER.NAME} has expelled the {CLAN} clan from the {KINGDOM}.");
        }
        else
        {
          switch (supportStatus)
          {
            case KingdomDecision.SupportStatus.Majority:
              parent = new TextObject("{=rd229FYG}{RULER.NAME} has expelled the {CLAN} clan from the {KINGDOM} with the support of {?RULER.GENDER}her{?}his{\\?} council.");
              break;
            case KingdomDecision.SupportStatus.Minority:
              parent = new TextObject("{=G3qGLAeQ}{RULER.NAME} has expelled the {CLAN} clan from the {KINGDOM} against the wishes of {?RULER.GENDER}her{?}his{\\?} council.");
              break;
            default:
              parent = new TextObject("{=m6OVl6Dg}{RULER.NAME} has expelled the {CLAN} clan from the {KINGDOM}, with {?RULER.GENDER}her{?}his{\\?} council evenly split on the matter.");
              break;
          }
        }
      }
      else if (this.IsSingleClanDecision())
      {
        parent = new TextObject("{=mvkKP6OE}{RULER.NAME} chose not to expel the {CLAN} clan from the {KINGDOM}.");
      }
      else
      {
        switch (supportStatus)
        {
          case KingdomDecision.SupportStatus.Majority:
            parent = new TextObject("{=yBL3TzXw}{RULER.NAME} chose not to expel the {CLAN} clan from the {KINGDOM} with the support of {?RULER.GENDER}her{?}his{\\?} council.");
            break;
          case KingdomDecision.SupportStatus.Minority:
            parent = new TextObject("{=940TwBPs}{RULER.NAME} chose not to expel the {CLAN} clan from the {KINGDOM} over the objections of {?RULER.GENDER}her{?}his{\\?} council.");
            break;
          default:
            parent = new TextObject("{=Oe1NdVLe}{RULER.NAME} chose not to expel the {CLAN} clan from the {KINGDOM} with {?RULER.GENDER}her{?}his{\\?} council evenly split on the matter.");
            break;
        }
      }
      parent.SetTextVariable("CLAN", this.ClanToExpel.Name);
      parent.SetTextVariable("KINGDOM", this.OldKingdom.Name);
      StringHelpers.SetCharacterProperties("RULER", this.OldKingdom.Leader.CharacterObject, parent);
      return parent;
    }

    public override DecisionOutcome GetQueriedDecisionOutcome(
      MBReadOnlyList<DecisionOutcome> possibleOutcomes)
    {
      return possibleOutcomes.FirstOrDefault<DecisionOutcome>((Func<DecisionOutcome, bool>) (t => ((ExpelClanFromKingdomDecision.ExpelClanDecisionOutcome) t).ShouldBeExpelled));
    }

    public class ExpelClanDecisionOutcome : DecisionOutcome
    {
      [SaveableField(100)]
      public readonly bool ShouldBeExpelled;

      internal static void AutoGeneratedStaticCollectObjectsExpelClanDecisionOutcome(
        object o,
        List<object> collectedObjects)
      {
        ((DecisionOutcome) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
      }

      protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
      {
        base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      }

      internal static object AutoGeneratedGetMemberValueShouldBeExpelled(object o)
      {
        return (object) ((ExpelClanFromKingdomDecision.ExpelClanDecisionOutcome) o).ShouldBeExpelled;
      }

      public ExpelClanDecisionOutcome(bool shouldBeExpelled)
      {
        this.ShouldBeExpelled = shouldBeExpelled;
      }

      public override TextObject GetDecisionTitle()
      {
        TextObject decisionTitle = new TextObject("{=kakxnaN5}{?SUPPORT}Yes{?}No{\\?}");
        decisionTitle.SetTextVariable("SUPPORT", this.ShouldBeExpelled ? 1 : 0);
        return decisionTitle;
      }

      public override TextObject GetDecisionDescription()
      {
        return this.ShouldBeExpelled ? new TextObject("{=s8z5Ugvm}The clan should be expelled") : new TextObject("{=b2InhEeP}We oppose expelling the clan");
      }

      public override string GetDecisionLink() => (string) null;

      public override ImageIdentifier GetDecisionImageIdentifier() => (ImageIdentifier) null;
    }
  }
}
